package com.imvc.service;

import com.imvc.util.CommonUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 创建业务类的代理工程
 *
 * @author ml
 * @create 2017-10-18--16:42
 */
public class ServiceFactory {
    private static final String DEFAULT_IMPL_PACKAGE_NAME = "impl";

    private static Map<Class<?>, Object> map = new HashMap<>();


    /**
     * 工程方法
     * @param type 业务逻辑对象的类型
     * @return 业务逻辑对象的代理对象
     */
    public static synchronized Object factory(Class<?> type) {

        if (map.containsKey(type)) {
            return map.get(type);
        } else {
            try {
                Object serviceObj = Class.forName(
                        type.getPackage().getName() + "." + DEFAULT_IMPL_PACKAGE_NAME + "." + type.getSimpleName() + CommonUtil.capitalize(DEFAULT_IMPL_PACKAGE_NAME));

                map.put(type,ServiceProxy.getProxyInstance(serviceObj));
                return serviceObj;
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}
